<template>
  <div class="login-wrap">
    <div class="ms-title">chain</div>
    <div class="ms-login">
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="0px"
        class="demo-ruleForm background-color: rgba(155, 218, 255, 0.424);"
      >
        <div v-if="errorInfo">
          <span>{{ errInfo }}</span>
        </div>
        <el-form-item prop="username">
          <el-input v-model="ruleForm.username" placeholder="账号"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            type="password"
            placeholder="密码"
            v-model="ruleForm.password"
            @keyup.enter.native="submitForm('ruleForm')"
          ></el-input>
        </el-form-item>
        <el-form-item prop="validate">
          <el-input
            v-model="ruleForm.validate"
            class="validate-code"
            placeholder="验证码"
          ></el-input>
          <div class="code" @click="refreshCode">
            <s-identify :identifyCode="identifyCode"></s-identify>
          </div>
        </el-form-item>
<!--        <el-form-item class="face">-->
<!--          &lt;!&ndash; video/ogg, video/flv, video/avi, video/wmv, video/rmvb,  &ndash;&gt;-->
<!--          <el-upload-->
<!--            ref="videoUpload"-->
<!--            :multiple="false"-->
<!--            :on-success="videoSuccess"-->
<!--            :on-error="videoFail"-->
<!--            :on-change="updateVideo"-->
<!--            :limit="1"-->
<!--            :auto-upload="false"-->
<!--            accept="video/mp4"-->
<!--            :action="videoUrl"-->
<!--          >-->
<!--            <el-button size="small" type="primary">点击上传人脸视频</el-button>-->
<!--          </el-upload>-->
<!--          <el-form-item v-if="is_start">-->
<!--            <li v-if="faceCorrect == 1">人脸符合！</li>-->
<!--            <li v-if="faceCorrect == 0">人脸不符合！</li>-->
<!--            <li v-if="faceCorrect == 2">人脸识别中...</li>-->
<!--            <li v-if="faceCorrect == 3">无人脸！</li>-->
<!--          </el-form-item>-->
<!--        </el-form-item>-->
        <div class="login-btn">
          <el-button
            :loading="loading"
            type="primary"
            @click="submitForm('ruleForm')"
            >登录</el-button
          >
        </div>
        <p class="register" @click="handleCommand()">注册</p>
      </el-form>
    </div>
  </div>
</template>

<script>
import axios from "axios";
import { verifyUser } from "@/api/userInfo";
export default {
  name: "login",
  data() {
    return {
      videoUrl: "http://127.0.0.1:5000/verifyFace",
      is_start: false,
      faceCorrect: 3,
      inputNameId: 1,
      loading: false,
      identifyCodes: "1234567890",
      identifyCode: "",
      errorInfo: false,
      ruleForm: {
        username: "",
        password: "",
        validate: "",
      },
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
        ],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
        validate: [
          { required: true, message: "请输入验证码", trigger: "blur" },
        ],
      },
    };
  },
  mounted() {
    this.identifyCode = "";
    this.makeCode(this.identifyCodes, 4);
  },
  methods: {
    submitForm(formName) {
      const self = this;
      console.log("json->" + JSON.stringify(self.ruleForm));
      self.$refs[formName].validate((valid) => {
        if (valid) {
          if (self.ruleForm.validate != this.identifyCode) {
            this.$message({
              type: "error",
              message: "验证码错误TT",
            });
            return false;
          }
          this.loading = true;
          verifyUser({
            username: self.ruleForm.username,
            password: self.ruleForm.password,
          }).then((response) => {
            console.log("收到id->" + response);
            this.loading = false;
            if (response == null) {
              self.errorInfo = true;
              self.errInfo = "该用户不存在或密码错误";
              console.log("该用户不存在或密码错误");
            } else {
              // self.faceCorrect = 2;
              // self.is_start = true;
              // let fd = new FormData();
              // console.log(self.videoData.raw);
              // fd.append("video", self.videoData.raw);
              // fd.append("id", response);
              // axios
              //   .post(
              //     "http://127.0.0.1:5000/verifyFace",
              //     // 跨域条件
              //     fd,
              //     {
              //       "Content-Type":
              //         "application/x-www-form-urlencoded; charset=UTF-8; multipart/form-data",
              //       "X-Requested-With": "XMLHttpRequest",
              //     }
              //   )
              //   .then(function (res) {
              //     console.log(res.data)
              //     console.log(typeof(res.data))
              //     console.log(typeof(1))
              //     if (res.data === 1) {
              //       self.faceCorrect = 1;
              //       console.log("登录成功");
              //       // store.dispact 调用的是application/web/src/store/modules/account.js 参数是id
              //       self.$store.dispatch("account/login", response).then(() => {
              //         self.$router.push("/myItem");
              //       });
              //     } else {
              //       self.faceCorrect = 0;
              //     }
              //   });
              self.$store.dispatch("account/login", response).then(() => {
                self.$router.push("/myItem");
              });
            }
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    // send() {
    //   axios({
    //     method: "get",
    //     url: "http://127.0.0.1:5000/index",
    //   }).then(function (request) {});
    // },

    videoSuccess(res, file, fileList) {
      console.log("视频上传成功 ", res);
      console.log(file);
      console.log(fileList); // 这里可以获得上传成功的相关信息
    },
    videoFail(err, file, fileList) {
      console.log("视频上传失败", err);
    },
    updateVideo(file) {
      this.videoData = file;
    },
    handleCommand() {
      this.$router.push("/register");
    },
    randomNum(min, max) {
      return Math.floor(Math.random() * (max - min) + min);
    },
    refreshCode() {
      this.identifyCode = "";
      this.makeCode(this.identifyCodes, 4);
    },
    makeCode(o, l) {
      for (let i = 0; i < l; i++) {
        this.identifyCode +=
          this.identifyCodes[this.randomNum(0, this.identifyCodes.length)];
      }
      console.log(this.identifyCode);
    },
  },
};
</script>

<style scoped>
.face {
  position: relative;
  width: 100%;
  text-align: left;
  font-size: 20px;
}
.login-wrap {
  background-color: rgba(155, 218, 255, 0.424);
  position: relative;
  width: 100%;
  height: 100%;
}
.ms-title {
  position: absolute;
  top: 50%;
  width: 100%;
  margin-top: -230px;
  text-align: center;
  font-size: 30px;
  color: #fff;
}
.ms-login {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 360px;
  height: 240px;
  margin: -150px 0 0 -190px;
  padding: 40px;
  border-radius: 5px;
  background: #fff;
}
.ms-login span {
  color: red;
}
.login-btn {
  text-align: center;
}
.login-btn button {
  width: 100%;
  height: 36px;
}
.code {
  width: 112px;
  height: 35px;
  border: 1px solid #ccc;
  float: right;
  border-radius: 2px;
}
.validate-code {
  width: 136px;
  float: left;
}
.register {
  font-size: 14px;
  line-height: 30px;
  color: #999;
  cursor: pointer;
  float: right;
}
</style>